/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sasp_messages;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author mauriello
 */
public class PINGCommand extends SASPCommand {
    public PINGCommand( ) {
        super( );
        this.setType(PING);
    }

    public PINGCommand( int _o, int _d, int _m ) {
        super( _o, _d, _m );
        this.setType(PING);
        this.setCommandBody(new JSONObject());
    }

    public PINGCommand( String _s ) {
        super( _s );
        System.out.println("PNG - Ping Message Received");
    }

    @Override
    public String toString( ) {
        boolean trouble = false;
        JSONObject retVal = new JSONObject();
        try {
            retVal.put("SASPv1Command", this.getType());
            retVal.put("OriginatorID", Integer.toString(this.getOriginatorID()));
            retVal.put("DestinationID", Integer.toString(this.getDestinationID()));
            retVal.put("MessageID", Integer.toString(this.getMessageID()));
            retVal.put("Timestamp", this.getDateTime().toString());
            retVal.put("CommandBody", this.getCommandBody());
        } catch (JSONException ex) {
            trouble = true;
            Logger.getLogger(PINGCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
        if( trouble ) {
            return null;
        } else {
            return retVal.toString();
        }
    }

    @Override
    public SASPReply execute() {
        System.out.println("PNG - ACK Reply Generated");
        return new ACKReply(this.getDestinationID(),this.getOriginatorID(),this.getMessageID());
    }
}
